package com.bi.onlineEducation.web.controller;

import com.bi.onlineEducation.utils.FtpUtil;
import com.bi.onlineEducation.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author: chenYuTao
 * @Time: 2022/4/19 23:14
 * @Description:
 **/
@RestController
@Api(tags = "文件上传", description = "文件上传")
public class UploadController {

    @Value("${upload.imgPath}")
    public String uploadImgPath;
    @Value("${upload.videoPath}")
    public String uploadVideoPath;
    @Value("${upload.servicePath}")
    public String uploadServicePath;

    @Resource
    private FtpUtil ftpUtil;

    /**
     * TODO 大文件上传限制，待完成
     *
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @ApiOperation("ftp上传文件到容器")
    @PostMapping(value = "/uploadFile")
    public ResultCode uploadFile(MultipartFile file, HttpServletRequest request) throws IOException {
        try {
            String result = ftpUtil.uploadFile(file.getOriginalFilename(), file.getInputStream());
            return ResultCode.success(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param fileName
     * @return
     * @throws IOException
     */
    @ApiOperation("删除ftp文件")
    @PutMapping(value = "/deleteFile")
    public ResultCode deleteFile(String fileName) {
        Boolean result = ftpUtil.deleteFile(fileName);
        return result ? ResultCode.success(true) : ResultCode.success(false);
    }

    @ApiOperation("上传文件到本地容器")
    @PostMapping(value = "/uploadFile2")
    public ResultCode uploadFile2(MultipartFile file, HttpServletRequest request) throws IOException {

        String filePath = fileUpload(file, request);
        return ResultCode.success(filePath);
    }

    /**
     * 返回虚拟文件访问路径
     *
     * @param file
     * @param request
     * @return
     */
    public String fileUpload(MultipartFile file, HttpServletRequest request) throws IOException {
        if (file.isEmpty()) {
            System.out.println("上传的文件为空！");
        }
        // 文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀名
        String extension = ".".concat(fileName.substring(fileName.lastIndexOf(".") + 1));

        // 生成一个新的文件名
        fileName = String.valueOf(System.currentTimeMillis()).concat(extension);
        // 上传文件的路径,如果没有将自动创建
        String resource = uploadVideoPath + fileName;

        File dest = new File(resource);
        // 检测文件目录是否存在 不存在则创建
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            System.out.println("上传文件成功！" + uploadServicePath.concat(fileName));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传文件失败！");
        }
        return uploadServicePath.concat(fileName);
    }

}
